#!/bin/sh
# @author lixiyong01
# @date 2023.03.16

APP='app_zp_rock_health_score_2onduty'
RUNDATE=`date -d "-1 days" "+%Y%m%d"`

sql="select dt,tag,event_id,responser,subject,description
     from hdp_lbg_supin_zplisting.app_zp_rock_health_score_ticket
     where dt=${RUNDATE} and category='个人健康分'
     	and (team like '平台技术部|数据应用技术部%' or responser in('xuhualei','caoyu01','zhaoxin06','chengjinman','niuqian01','chenwei35','yangning02','jihailong','lishujie'))
     "
echo "sql ${sql}"

# 报警调用
function send_onduty() {
    event_id=$1
    responser=$2
    subject=$3
    description=$4

    echo "subject:"$subject " description:"$description " responser:"responser
    curl -L -X POST 'http://app-onduty-zp.58corp.com/onduty/app/v1/api/addEvent' \
    -H 'Content-Type: application/json' \
    -d '{
        "agentId": "cc7cafe1-c906-4f6b-ae14-a4aa503d89ff",
        "agentSecret": "a90e2ea2-8d96-4579-b976-1c0476cc16b9",
        "endpoint": "offline-checker",
        "resolvegroupId": "rg-20230201172722511fsnpuq",
        "ctiId": "2023020117265984859mhrd",
        "eventId": "'"${event_id}"'",
        "eventTs": "",
        "eventType":"softwareFault",
        "severity": 3,
        "isNotifyRg":false,
        "subject": "'"${subject}"'",
        "description": "'"${description}"'",
        "responser":  "'"${responser}"'"
    }'
    #curl -L -X POST 'https://hrgluffy-api.58corp.com/api/meishi/hdfs' \
    #    -H 'Content-Type: application/json' \
    #    -d '{
    #        "description": "'"${description}"'"
    #    }'
}

#### main process ####
mkdir -p ./tmp
results_file="./tmp/${APP}_${RUNDATE}.dat"

hive -e "set mapreduce.job.queuename=root.offline.hdp_lbg_supin.normal;SET hive.cli.print.header=false;${sql}" >> ${results_file}

i=0
cat ${results_file} | while read line;
do
    echo "line: ${line}"
	row=${line};
	items=($row)
    dt=${items[0]}
	tag=${items[1]}
	event_id=${items[2]}
	responser=${items[3]}
	subject='[试行]'${items[4]}'-'${tag}
    url="http://star-zp.58corp.com/sre/index.html#/star/cost/health_score_list2/${tag}/${dt}/0/${responser}"
	description=${items[5]}'帮助：https://docs.qq.com/doc/DSGJaZFZhVXpvbGFS''健康分：'${url}
	let i++;
	echo "[${i}]responser:${responser},subject:${subject}"
	send_onduty "$event_id" "$responser" "$subject" "$description"
done

#rm -f ${results_file}
